﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;
using System.Text;
using Cit.CDE.Net.Service;
using System.Net.Mail;

public partial class main_admin_ChooseUser : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    private String EM;
    string u;
    string msg_type;
    string source_id;
    /// <summary>
    /// 操作员ID
    /// </summary>
    private string opId;

    protected void Page_Load(object sender, EventArgs e)
    {
        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        opId = op.SysID;


        Master.strHc = "0";
        //设置母版页控件显示
        u = Request.QueryString["U"];
        hidU.Value = u;
        ((Literal)Master.FindControl("lblpath")).Text = "选择通知人";

       // 1:网上申请,2:培训
        msg_type = (string)Request.QueryString.Get("MSG_TYPE");
        source_id = (string)Request.QueryString.Get("SOURCE_ID");      
        

        EM = Request.QueryString[WEBConstants.EM_QUERY_STRING];

        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        Master.sc = this.CreatSc(em);
        //设置母版页控件事件
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ItemDataBound += new DataGridItemEventHandler(this.itemDataBound);
    }
    /// <summary>
    /// 操作事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void itemDataBound(object sender, DataGridItemEventArgs e)
    {
        //控制母版页控件显示内容
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns[((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns.Count - 1].Visible = false;
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        string strErr = string.Empty;
        string strMsg = string.Empty;

        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];

        List<string> list = new List<string>();
        if (S_listEntity.Count > 0)
        {
            if (Master.listIndex.Count > 0)
            {
                string MSG_CONTENT = "";
                string MSG_TITLE = "";
                string SOURCE_ID = source_id;
                string MSG_TYPE = msg_type;

                //网上申请通知处理
                if (MSG_TYPE == "1")
                {
                    CDEEntityManager em_source = cde.cache.CDEFactory.getEMByFullName("WEB.LORE_MANAGE");
                    CDESearchCriteria sc_source = new CDESearchCriteria(em_source);
                    sc_source.addCondition("ID", source_id);
                    List<CDEEntity> listEntity_source = CDEService.LoadEntities(em_source, sc_source);
                    //资料ID是否存在
                    if (listEntity_source.Count > 0)
                    {
                        MSG_TITLE = "网上申请通知：" + Util.objectToString(listEntity_source[0].get("LORE_TITLE"));
                        MSG_CONTENT = Util.objectToString(listEntity_source[0].get("LORE_CONTENT"));
                    }
                }
                else if (MSG_TYPE == "2"){//培训申请通知
                    CDEEntityManager em_source = cde.cache.CDEFactory.getEMByFullName("WEB.TRAINING_MANAGE");
                    CDESearchCriteria sc_source = new CDESearchCriteria(em_source);
                    sc_source.addCondition("ID", source_id);
                    List<CDEEntity> listEntity_source = CDEService.LoadEntities(em_source, sc_source);
                    //资料ID是否存在
                    if (listEntity_source.Count > 0)
                    {
                        string teacher = Util.objectToString(listEntity_source[0].get("TEACHER__NAME"));
                        string start_date = Util.objectToString(listEntity_source[0].get("START_DATE"));
                        string end_date = Util.objectToString(listEntity_source[0].get("END_DATE"));
                        string msg_content = Util.objectToString(listEntity_source[0].get("MEMO"));
                        MSG_TITLE = "培训申请通知：" + teacher + "[" + start_date + "]";
                        MSG_CONTENT = msg_content;
                    }

                }
                else if (MSG_TYPE == "3")
                {//市场活动通知
                    CDEEntityManager em_source = cde.cache.CDEFactory.getEMByFullName(WEBConstants.EM_MARKET_ACTIVITY_MANAGE);
                    CDESearchCriteria sc_source = new CDESearchCriteria(em_source);
                    sc_source.addCondition("ID", source_id);
                    List<CDEEntity> listEntity_source = CDEService.LoadEntities(em_source, sc_source);
                    //市场活动 是否存在
                    if (listEntity_source.Count > 0)
                    {
                        string titile = Util.objectToString(listEntity_source[0].get("TITILE"));
                        string start_date = Util.objectToString(listEntity_source[0].get("START_DATE"));
                        string end_date = Util.objectToString(listEntity_source[0].get("END_DATE"));
                        string msg_content = Util.objectToString(listEntity_source[0].get("MEMO"));
                        MSG_TITLE = "市场活动通知：" + titile + "[" + start_date + "]";
                        MSG_CONTENT = msg_content;
                    }

                }

                //通知消息对象列表构建
                List<CDEEntity> listEntity_MSG = new List<CDEEntity>();
                CDEEntityManager em_MSG = cde.cache.CDEFactory.getEMByFullName("WEB.MSGP_MANAGE");
                //当前时间
                string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/"); 
                foreach (int index in Master.listIndex)
                {
                    CDEEntity m_entity_MSG = CDEEMServiceFactory.getEMService(em_MSG).CreateEntity();
                    m_entity_MSG.Operation = CDEDataOperation.INSERT;
                    string RECEIVE_USER = Util.objectToString(S_listEntity[index].Id);
                    m_entity_MSG.set("RECEIVE_USER", RECEIVE_USER);

                    //发邮件
                    //-----------------------------------------------------------------------------
                    string EMAIL = Util.objectToString(S_listEntity[index].get("EMAIL"));
                     try
                        {
                            if (EMAIL != "") {
                                MailMessage mail = new MailMessage();
                                mail.To.Add(EMAIL);
                                mail.Subject = MSG_TITLE;
                                mail.Body = MSG_CONTENT;
                                mail.IsBodyHtml = true;

                                SmtpClient smtp = new SmtpClient();
                                smtp.Send(mail);
                            }
                        }
                     catch (Exception exx)
                     {
                         UserControl.Log.SaveException(exx);                        
                     }
                     //-----------------------------------------------------------------------------

                    m_entity_MSG.set("INSERT_USER", opId);
                    m_entity_MSG.set("INSERT_DATE", Now);
                    m_entity_MSG.set("MSG_CONTENT", MSG_CONTENT);
                    m_entity_MSG.set("MSG_TITLE", MSG_TITLE);
                    m_entity_MSG.set("SOURCE_ID", SOURCE_ID);
                    m_entity_MSG.set("MSG_TYPE", MSG_TYPE);
                    m_entity_MSG.set("STATE", "0");//1:已读,0:未读
                   
                    listEntity_MSG.Add(m_entity_MSG); 
                }


                //更新
                CDEService.UpdateEntities(em_MSG, listEntity_MSG);

                for (int index = 0; index < listEntity_MSG.Count; index++)
                {
                    if (listEntity_MSG[index].Result == CDEDataOperationResult.ERROR)
                    {
                        strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
                    }
                }


                if (strErr != string.Empty)
                {
                    strErr = strErr.Substring(0, strErr.Length - 1);
                    strMsg = Resources.ResourceErrMsg.DELETE_ERR;
                    strMsg = strMsg.Replace("{0}", strErr);
                    showMessageErrAndOk(strMsg);
                }
                else
                {
                    StringBuilder sbr = new StringBuilder();
                    sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
                    sbr.AppendFormat("closeWin()");
                    sbr.AppendFormat("</script>{0}", Environment.NewLine);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
                }
        


             
            }
            else
            {
                showMessageErrAndOk("请选择收件人！");
            }
        }
        else
        {
            showMessageErrAndOk("无收件人信息！");
        }
    }
}
